/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {
    loadList('/v1/teacher/retire/list.json');

    $('#delete-retire-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        $(this).find('.retire-id').val(id);
    });
    $('#delete-retire-dialog').on('hide.bs.modal', function(){
        $(this).find('.retire-id').val('');
    });
    $('#delete-retire-dialog .btn-save').on('click', function(){
        var id = $('#delete-retire-dialog').find('.retire-id').val();
        sendAjax('/v1/teacher/delete/retire', 'post', {id:id}, function(json){
            $('#delete-retire-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#view-retire-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var that = $(this);
        sendAjax('/v1/teacher/retire/detail.json', 'get', {id:id}, function(json){
            var retire = json.body.data;
            that.find('.retire-id').val(id);
            that.find('.apart-id').val(retire.apart_id);
            that.find('.apart-name').text(retire.name);
            var type = getRetireType(retire.type);
            that.find('.retire-type').text(type);
            that.find('.checkout-at').text(retire.checkout_at);
            var is_rent_pay = retire.is_rent_pay ? '已付' : '未付';
            var is_water_fee_pay = retire.is_water_fee_pay ? '已付' : '未付';
            var is_electric_fee_pay = retire.is_electric_fee_pay ? '已付' : '未付';
            var is_gas_fee_pay = retire.is_gas_fee_pay ? '已付' : '未付';
            var is_device_safe = retire.is_device_safe ? '完好' : '有损';
            that.find('.is-rent-pay').text(is_rent_pay);
            that.find('.is-water-fee-pay').text(is_water_fee_pay);
            that.find('.is-electric-fee-pay').text(is_electric_fee_pay);
            that.find('.is-gas-fee-pay').text(is_gas_fee_pay);
            that.find('.is-device-safe').text(is_device_safe);
            that.find('.retire-remark').html(retire.remark);
        });
    });
    $('#view-retire-dialog').on('hide.bs.modal', function(){});

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url) {
    $("#retire-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'name',
            title: '公寓名称'
        }, {
            field: 'checkout_at',
            title: '退房时间'
        }, {
            field: 'type',
            title: '类型',
            formatter: function (value) {
                return getRetireType(value);
            }
        }, {
            field: 'user_id',
            title: '承租人',
            visible: false
        }, {
            field: 'created_by',
            title: '发起人',
            visible: false
        }, {
            field: 'state_show',
            title: '状态'
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-retire" data-toggle="modal" data-target="#view-retire-dialog" data-id="'+row.id+'">详情</a>';
                if (row.created_by == row.user_id) action += '<a class="btn btn-xs btn-danger delete-retire" data-toggle="modal" data-target="#delete-retire-dialog" data-id="'+row.id+'">删除</a>';
                return action;
            }
        }]
    });
}

function getRetireType(type) {
    switch (type) {
        case 1 : return '到期退房';
        case 2 : return '提前退房';
        case 3 : return '强制退房';
        default: return '到期退房';
    }
}